<template>
    <el-row class="login-container">
        <el-col :lg="16" :md="12" class="left">
            <div>
                <div>博码固定资产系统 欢迎光临！</div>
                <div class="">
                    支持Barcode（条形码）、Qrcode（二维码）、RFID（无线射频技术）！
                </div>
            </div>
        </el-col>

        <el-col :lg="8" :md="12" class="right">
            <img src="../assets/images/BOMA-LOGO.jpg" alt="" class="w-[150px] pb-15" />
            <h2>欢迎回来</h2>
            <div>
                <span class="line"></span>
                <span> 用户登录 </span>
                <span class="line"></span>
            </div>
            <el-form :rules="rules" ref="formRef" :model="form" class="w-[250px]">
                <el-form-item prop="username">
                    <el-input placeholder="请输入用户名" v-model="form.username">
                        <template #prefix>
                            <el-icon>
                                <User />
                            </el-icon>
                        </template>
                    </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input type="password" show-password placeholder="请输入密码" v-model="form.password">
                        <template #prefix>
                            <el-icon>
                                <Lock />
                            </el-icon>
                        </template>
                    </el-input>
                </el-form-item>

                <el-form-item>
                    <el-button round="round" color="#6366f1" class="w-[250px] mt-4" type="primary" @click="onSubmit" :loading="loading">登录</el-button>
                </el-form-item>
            </el-form>


        </el-col>
    </el-row>
</template>

<script setup>
import { ref, reactive, onMounted, onBeforeUnmount } from "vue";
// 导入消息通知组件封装类
import { toast } from "~/composables/util";
// 导入路由
import { useRouter } from "vue-router";
// 导入 useStore
import { useStore } from "vuex";

const store = useStore();
const router = useRouter();

// 获取表单信息
const form = reactive({
    username: "",
    password: "",
});

// 校验规则
const rules = {
    username: [{ required: true, message: "用户名不能为空", trigger: "blur" }],
    password: [{ required: true, message: "密码不能为空", trigger: "blur" }],
};
const formRef = ref(null);
// 登入按钮状态 默认false
const loading = ref(false);
const onSubmit = () => {
    formRef.value.validate((valid) => {
        if (!valid) {
            console.log("表单校验失败");
            return false;
        }
        loading.value = true;
        // 登入
        store
            .dispatch("login", form)
            .then((res) => {
                toast("恭喜您！登入成功", "success", 3000);
                // 跳转主页
                router.push("/");
            })
            .finally(() => {
                loading.value = false;
            });
    });
};

// 监听回车事件
function onKeyUp(e) {
    if (e.key == "Enter") onSubmit();
}
// 添加键盘监听 当组件挂在完成之后
onMounted(() => {
    document.addEventListener("keyup", onKeyUp);
});
// 移除键盘监听 当组件卸载完成之后
onBeforeUnmount(() => {
    document.removeEventListener("keyup", onKeyUp);
});
</script>

<style scoped>
.login-container {
    @apply min-h-screen bg-indigo-500;
}

.login-container .left {
    @apply flex items-center justify-center;
}

.login-container .right {
    @apply bg-light-200 flex items-center justify-center flex-col;
}

.left>div>div:first-child {
    @apply font-bold text-5xl text-light-100 mb-4;
}

.left>div>div:last-child {
    @apply text-gray-200 text-base;
}

.right>h2 {
    @apply font-bold text-3xl text-gray-800;
}

.right>div {
    @apply flex items-center justify-center my-5 text-gray-500 space-x-2;
}

.right .line {
    @apply h-[1px] w-16 bg-gray-200;
}
</style>